package com.example.administrator.xiaobo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Administrator on 2017/2/8.
 */

public abstract class RecyclerBaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private List<T> datas;
    private LayoutInflater layoutInflater;
    private Context context;
    protected int mLayoutResId;
    private OnItemClickListener mOnItemClickListener = null;
    public interface OnItemClickListener {
        void OnClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public RecyclerBaseAdapter(Context context,List<T> datas,int mLayoutResId){
        this.datas=datas;
        this.context=context;
        this.mLayoutResId=mLayoutResId;
        layoutInflater= LayoutInflater.from(context);

    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(mLayoutResId, null, false);
        return new BaseViewHolder(view,mOnItemClickListener);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
       T t=getItem(position);
        bindData(holder,t);
    }

    @Override
    public int getItemCount() {
        if (datas == null && datas.size() <= 0)
            return 0;
        return datas.size();
    }
    public T getItem(int postion) {
        return datas.get(postion);
    }

    public abstract void bindData(BaseViewHolder holder, T t);

    public List<T> getDatas(){

        return  datas;
    }
    public void addData(List<T> datas){

        addData(0,datas);
    }

    public void addData(int position,List<T> datas){
        if(datas !=null && datas.size()>0) {

            this.datas.addAll(datas);
            this.notifyItemRangeChanged(position, datas.size());
        }
    }
    public void clearData(){
        int itemCount = datas.size();
        datas.clear();
        this.notifyItemRangeRemoved(0,itemCount);
    }
}
